<?php

declare(strict_types=1);

namespace app\user\controller\api;

use think\Request;
use sheep\controller\Api;
use sheep\exception\SheepException;
use app\user\model\WalletLog as UserWalletLogModel;

class WalletLog extends Api
{
    public function list(Request $request)
    {
        $type = $request->param('type', 'money');
        $tab = $request->param('tab', 'all');
        $list_rows = $request->param('list_rows', 10);
        $date = $request->param('date');
        $user = $this->auth()->user();
        $where[] = ['user_id', '=', $user->id];

        switch ($tab) {
            case 'income':
                $where[] = ['amount', '>', 0];
                break;
            case 'expense':
                $where[] = ['amount', '<', 0];
                break;
        }

        $income = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '>', 0)->whereBetweenTime('create_time', $date[0], $date[1])->sum('amount');
        $expense = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '<', 0)->whereBetweenTime('create_time', $date[0], $date[1])->sum('amount');
        $logs = UserWalletLogModel::where($where)->{$type}()->whereBetweenTime('create_time', $date[0], $date[1])->order('create_time', 'desc')->paginate($list_rows);
        return success('获取成功', ['list' => $logs, 'income' => $income, 'expense' => $expense]);
    }
}
